package com.sumainfor.pc.epc.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sumainfor.common.entity.MoSupplierObject;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.SystemUtils;
import com.sumainfor.common.utlis.MoSupplierObjectUtil;
import com.sumainfor.pc.epc.dao.WorkBenchMapper;
import com.sumainfor.pc.epc.feign.GetUserInfoFeign;
import com.sumainfor.pc.epc.service.WorkBenchService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-04-12 16:48
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Log4j2
public class WorkBenchServiceImpl implements WorkBenchService {

    @Autowired
    private WorkBenchMapper mapper;

    @Autowired
    private GetUserInfoFeign feign;

    /**
     * 主页列表显示的
     *
     * @return
     */
    @Override
    public JsonResult getWorkData() {
        //获取姓名
        String userName = ContextUtils.getSysUser().getName();
        CustomMap data = CustomMap.create("userName", userName);
        //获取重要状态提醒
        //1.查询需采购装修单
        data.put("BuyerCount", mapper.selectWaitOrderCount());
        //2.查询配置变更待处理的
        data.put("ChangeCount", mapper.selectConfigChangeCount());
        //3.查询被驳回打款单
        data.put("GoBackRemitCount", mapper.selectGoBackRemit());
        //4.查询未结清账单
        data.put("WaitMoneyCount", mapper.selectWaitMoneyOrder());
        //5.查询低库存备件预警
        data.put("StockWarningCount", mapper.selectStockWarning().stream().filter(item -> {
            List<MoSupplierObject> moList = MoSupplierObjectUtil.convertJsonToList(MapUtils.getString(item, "supplier"));
            for (MoSupplierObject mo : moList) {
                if (mo.isWarning()) {
                    return true;
                }
            }
            return false;
        }).count());

        //查询待处理订单
        //1.查询装修订单
        List<Map<String, Object>> zxOrder = mapper.selectZXOrder();
        List<Map<String, Object>> receiveOrder = mapper.selectChangeOrder();
        List<Map<String, Object>> waritList = Lists.newArrayList();
        Set<String> userIds = Sets.newHashSet();
        for (Map<String, Object> map : zxOrder) {
            map.put("type", 1);
            waritList.add(map);
            userIds.add(MapUtils.getString(map, "SysUserId"));
        }
        for (Map<String, Object> map : receiveOrder) {
            map.put("type", 2);
            waritList.add(map);
            userIds.add(MapUtils.getString(map, "SysUserId"));
        }
        //查询用户名称 和联系电话
        if (CollectionUtils.isNotEmpty(userIds)) {
            JsonResult result = feign.getUserInfo(userIds);
            if (result.isSuccess() == false) {
                return JsonResult.error("查询用户信息失败");
            }
            List<Map<String, Object>> list = (List<Map<String, Object>>) result.getResult();
            for (Map<String, Object> map : waritList) {
                String userId = (String) map.get("SysUserId");
                for (Map<String, Object> usr : list) {
                    if (MapUtils.getString(usr, "user_id").equals(userId)) {
                        map.put("username", usr.get("username"));
                        map.put("mobile", usr.get("mobile"));
                        break;
                    }
                }
            }
        }


        data.put("WaitList", waritList);

        //2.查询待处理领用单

        data.put("ReceiveOrder", mapper.selectReceiveOrder().stream().map(item -> {
            JsonResult jsonResult = feign.getUserInfo(Lists.newArrayList(MapUtils.getString(item, "SysUserId")));
            if (jsonResult.isSuccess() == false) {
                throw new TCException("获取用户信息失败:" + SystemUtils.getLineNumber());
            }
            List<Map<String, Object>> jsonResultResult = (List<Map<String, Object>>) jsonResult.getResult();
            if (CollectionUtils.isEmpty(jsonResultResult)) {
                throw new TCException("获取用户信息失败:" + SystemUtils.getLineNumber());
            }
            Map<String, Object> map = jsonResultResult.get(0);
            item.put("username", map.get("username"));
            item.put("mobile", map.get("mobile"));
            return item;
        }).collect(Collectors.toList()));
        return JsonResult.success(data);
    }

}
